/*
 * Copyright (c) 2014 . All Rights Reserved.
 */
package com.cgmnx.admin.service.rbac.impl;

import com.cgmnx.admin.common.bean.Limiter;
import com.cgmnx.admin.common.enums.ValidEnumType;
import com.cgmnx.admin.dao.entity.rbac.ResourceEntity;
import com.cgmnx.admin.dao.entity.rbac.ResourceTree;
import com.cgmnx.admin.dao.mapper.rbac.ResourceMapper;
import com.cgmnx.admin.service.rbac.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ResourceServiceImpl implements ResourceService {

//    @Autowired
    private ResourceMapper resourceMapper;

//    @Autowired
    private ResourceTree resourceTree;

    @Override
    public Integer save(ResourceEntity resourceEntity) {
        return resourceMapper.save(resourceEntity);
    }

    @Override
    public Integer update(ResourceEntity resourceEntity) {
        return resourceMapper.update(resourceEntity);
    }

    @Override
    public boolean batchDelete(List<Integer> list) {
        return resourceMapper.updateStatus(list, ValidEnumType.INVALID) > 0;
    }

    @Override
    public List<ResourceEntity> queryChildren(int parentId) {

        return resourceMapper.queryByParentId(parentId);
    }

    @Override
    public List<ResourceTree> queryTree(int parentId) {

        List<ResourceEntity> resourceEntities = queryChildren(parentId);

        if (CollectionUtils.isEmpty(resourceEntities)) {
            return Collections.emptyList();
        }

        return resourceTree.buildTree(resourceEntities, parentId);
    }

    @Override
    public List<ResourceEntity> queryPage(String searchName, Limiter limiter) {
        return resourceMapper.queryPage(searchName, limiter);
    }

    @Override
    public ResourceEntity query(Integer resourceId) {
        return resourceMapper.queryById(resourceId);
    }

    @Override
    public Integer count(String searchName) {
        return resourceMapper.count(searchName);
    }

    @Override
    public void updateStatus(List<Integer> idList, ValidEnumType invalid) {
        resourceMapper.updateStatus(idList, invalid);
    }

    @Override
    public List<ResourceEntity> queryByRoleId(Integer roleId) {
        return resourceMapper.queryByRoleId(roleId);
    }

    @Override
    public List<ResourceEntity> queryByUserId(String userId) {
        return resourceMapper.queryByUserId(userId);
    }

    @Override
    public void updateNodeCountIncr(Integer id) {
        resourceMapper.updateNodeCountIncr(id);
    }

    @Override
    public void updateNodeCountDecr(Integer id) {
        resourceMapper.updateNodeCountDecr(id);
    }

    @Override
    public List<ResourceEntity> queryAll() {
        return resourceMapper.queryAll();
    }

    @Override
    public List<ResourceEntity> queryList(List<Integer> resourceIdList) {
        return resourceMapper.queryList(resourceIdList);
    }
}
